package org.springblade.tms.module.inquirysheet.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.system.feign.IDictBizClient;
import org.springblade.tms.global.enums.ExpirationReasonEnum;
import org.springblade.tms.global.exception.CommonException;
import org.springblade.tms.module.inquirycarrier.service.IInquiryCarrierService;
import org.springblade.tms.module.inquirysheet.base.InquirySheetService;
import org.springblade.tms.module.inquirysheet.entity.TMInquirySheet;
import org.springblade.tms.module.inquirysheet.service.IInquirySheetService;
import org.springblade.tms.module.inquirysheet.vo.*;
import org.springblade.tms.module.sap.base.SapRequestService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.springblade.tms.global.constant.DictCodeConstant.SYSTEM_CONFIG;
import static org.springblade.tms.global.constant.RedisConstant.INQUIRY_FORM_VOID;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.inquirysheet.controller
 * @className: InquirySheetController
 * @author: yr
 * @description: 询价单控制器
 * @date: 2022/6/23 14:20
 */
@RestController
@RequestMapping("Inquiry/InquirySheet")
@Api(value = "询价单(新)-yr", tags = "询价单(新)-yr")
public class InquirySheetController {
    @Resource
    private InquirySheetService inquirySheetService;
    @Resource
    private IDictBizClient dictBizClient;
    @Resource
    private SapRequestService sapRequestService;

    /**
     * 分页 询价单列表
     */
    @ApiLog("询价单列表")
    @GetMapping("/list")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "询价单列表(新)", notes = "传入exceptionType")
    public R<IPage<InquirySheetRespVo>> list(InquirySheetReqVo inquirySheetReqVo, Query query) {
        return inquirySheetService.list(inquirySheetReqVo, query);
    }


    /**
     * 询价单详情
     */
    @ApiLog("询价单详情")
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "询价单详情(新)", notes = "传入id")
    public R<InquirySheetRespVo> detail(@RequestParam Long id) {
        return inquirySheetService.detail(id);
    }


    /**
     * 询价单承运商列表
     */
    @ApiLog("询价单承运商列表")
    @GetMapping("/carrier")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "询价单承运商列表(新)", notes = "传入inquiryCarrierListVo")
    public R<IPage<InquiryCarrierListRespVo>> carrier(InquiryCarrierListReqVo inquiryCarrierListReqVo, Query query) {
        return inquirySheetService.carrier(inquiryCarrierListReqVo, query);
    }


    /**
     * 询价单承运商绑定
     */
    @ApiLog("询价单承运商绑定")
    @PostMapping("/bind")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "询价单承运商绑定(新)", notes = "传入carrierId，inquiryId")
    public R<Boolean> bind(@RequestBody BindCarrierVo bindCarrierVo) {
        return inquirySheetService.bind(bindCarrierVo);
    }


    /**
     * 询价单承运商绑定
     */
    @ApiLog("询价单承运商报价")
    @PostMapping("/quotedPrice")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "询价单承运商报价(新)", notes = "传入id")
    public R<Boolean> quotedPrice(@RequestBody InquiryItemSaveVo inquiryItemSaveVo, @ApiParam("当前行数据id") @RequestParam Long id,
                                  @ApiParam("外贸时报价信息列表数据id") @RequestParam Long startId) {
        return inquirySheetService.quotedPrice(inquiryItemSaveVo, id, startId);
    }


    /**
     * 询价单承运商绑定
     */
    @ApiLog("询价单承运商报价")
    @PostMapping("/quotedPriceByAdmin")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "询价单管理员报价(新)", notes = "传入id")
    public R<Boolean> quotedPriceByAdmin(@RequestBody InquiryItemSaveVo inquiryItemSaveVoi, @ApiParam("当前行数据id") @RequestParam Long id,
                                         @ApiParam("报价信息列表数据id") @RequestParam Long startId) {
        return inquirySheetService.quotedPriceByAdmin(inquiryItemSaveVoi, id, startId);
    }


    /**
     * 询价单承运商绑定
     */
    @ApiLog("询价单报价列表")
    @GetMapping("/priceList")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "询价单报价列表(新)", notes = "传入id")
    public R<List<InquiryItemQueryVo>> priceList(@RequestParam Long id) {
        return inquirySheetService.priceList(id);
    }

    /**
     * 询价单承运商绑定
     */
    @ApiLog("外贸报价详情列表")
    @GetMapping("/priceDetailList")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "外贸报价详情列表(新)", notes = "传入id")
    public R<List<InquiryItemQueryVo>> priceDetailList(@ApiParam("询价单id") @RequestParam Long id, @ApiParam("承运商id") @RequestParam(required = false) String carrierId) {
        return inquirySheetService.priceDetailList(id, carrierId);
    }


    /**
     * 询价单承运商绑定
     *
     * @return
     */
    @ApiLog("历史报价列表查询")
    @GetMapping("/historicalPriceList")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "历史报价列表查询(新)", notes = "传入id")
    public R<List<HistoricalQuotationVO>> historicalPriceList(@RequestParam Long id) {
        return inquirySheetService.historicalPriceList(id);
    }

    /**
     * 询价单承运商绑定
     */
    @ApiLog("中标")
    @PostMapping("/winningTheBid")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "中标(新)", notes = "传入id")
    public R<Boolean> winningTheBid(@ApiParam("当前行数据id") @RequestParam Long id, @ApiParam("中标箱量") @RequestParam(required = false) BigDecimal winningBoxQuantity) {
        return inquirySheetService.winningTheBid(id, winningBoxQuantity);

    }

    /**
     * 询价单承运商绑定
     */
    @ApiLog("未中标(新)")
    @PostMapping("/didNotWinTheBid")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "未中标(新)", notes = "传入id")
    public R<Boolean> didNotWinTheBid(@ApiParam("当前行数据id") @RequestParam Long id) {
        return inquirySheetService.didNotWinTheBid(id);

    }

    /**
     * 询价单承运商绑定
     */
    @ApiLog("作废")
    @PostMapping("/invalid")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "作废(新)", notes = "传入id")
    public R<Boolean> invalid(@ApiParam("当前行数据id") @RequestParam Long id) {
        return inquirySheetService.invalid(id);

    }

    /**
     * 询价单承运商绑定
     */
    @ApiLog("获取单个配置")
    @PostMapping("/getConfig")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取单个配置", notes = "传入id")
    public R<String> getConfig(@ApiParam("key") @RequestParam String key) {
        String value = dictBizClient.getValue(SYSTEM_CONFIG, key).getData();
        return R.data(value);
    }

    /**
     * 询价单承运商绑定
     */
    @ApiLog("修改询价响应日期和截止日期")
    @PostMapping("/updateDate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "修改询价响应日期和截止日期")
    public R<String> updateDate(@RequestBody UpdateDateVO updateDateVO) {
        return inquirySheetService.updateDate(updateDateVO);
    }

    /**
     * 询价单承运商绑定
     */
    @ApiLog("手动提交OA接口")
    @GetMapping("/submitOA")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "手动提交OA接口")
    public R<Boolean> submitOA(@RequestParam String id) {
        Boolean aBoolean = inquirySheetService.toSapInquiry(Long.parseLong(id));
        if (aBoolean) {
            sapRequestService.quotedPrice(Long.parseLong(id));
        } else {
            CommonException.throwCommonException("询价单未全部询价完成，不允许提交!");
        }
        return R.data(true);
    }

    /**
     * 询价单承运商绑定
     */
    @ApiLog("询价单承运商列表")
    @GetMapping("/priceCarrierList")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "询价单承运商列表", notes = "传入id")
    public R<List<CarrierListVO>> priceCarrierList(@RequestParam Long id) {
        return inquirySheetService.priceCarrierList(id);
    }
}
